new: fix hgignore for real
authorSiddharth Agarwal <sid0@fb.com>
Mon, 31 Jul 2017 01:15:24 +0000 (18:15 -0700)
committerSiddharth Agarwal <sid0@fb.com>
Mon, 31 Jul 2017 01:28:18 +0000 (18:28 -0700)
There was an attempt to fix hgignore in #4158, but unfortunately the fix
was incorrect -- hgignore glob syntax is not in fact identical to
gitignore syntax.

To get rooted ignores in Mercurial we must use the regex-based syntax,
so we have no choice but to define a separate set of ignores.

src/cargo/ops/cargo_new.rs

index 31c308bb9b418776596cce72e9b383d3351cefac..6320498f64a65aa38f3b157c83f354bdd85e1be8 100644 (file)
@@ -390,9 +390,16 @@ fn mk(config: &Config, opts: &MkOptions) -> CargoResult<()> {
     let path = opts.path;
     let name = opts.name;
     let cfg = global_config(config)?;
+    // Please ensure that ignore and hgignore are in sync.
     let ignore = ["/target/\n", "**/*.rs.bk\n",
         if !opts.bin { "Cargo.lock\n" } else { "" }]
         .concat();
+    // Mercurial glob ignores can't be rooted, so just sticking a 'syntax: glob' at the top of the
+    // file will exclude too much. Instead, use regexp-based ignores. See 'hg help ignore' for
+    // more.
+    let hgignore = ["^target/\n", "glob:*.rs.bk\n",
+        if !opts.bin { "glob:Cargo.lock\n" } else { "" }]
+        .concat();
 
     let in_existing_vcs_repo = existing_vcs_repo(path.parent().unwrap(), config.cwd());
     let vcs = match (opts.version_control, cfg.version_control, in_existing_vcs_repo) {
@@ -412,8 +419,7 @@ fn mk(config: &Config, opts: &MkOptions) -> CargoResult<()> {
             if !fs::metadata(&path.join(".hg")).is_ok() {
                 HgRepo::init(path, config.cwd())?;
             }
-            let ignore = format!("syntax: glob\n{}", ignore);
-            paths::append(&path.join(".hgignore"), ignore.as_bytes())?;
+            paths::append(&path.join(".hgignore"), hgignore.as_bytes())?;
         },
         VersionControl::Pijul => {
             if !fs::metadata(&path.join(".pijul")).is_ok() {